home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / HFS- / SerialFSServ / SerialFSServ.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  25.2 KB  |  1,181 lines

  1. /*
  2.     File:        MyCaptureAppShell.c
  3.     
  4.     Contains:    MyCaptureApp shell.
  5.  
  6.     Written by:    John Wang
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <1>        04/04/94    JW        Created.
  13.  
  14.     To Do:
  15.         Wait till leave word rectangle
  16.         little L processor that Kat doesn't think should really be neccessary
  17.         
  18.     
  19. */
  20.  
  21.  
  22. #include    <Types.h>
  23. #include    <Memory.h>
  24. #include    <QuickDraw.h>
  25. #include    <Palettes.h>
  26. #include    <QDOffscreen.h>
  27. #include    <Errors.h>
  28. #include    <Fonts.h>
  29. #include    <Dialogs.h>
  30. #include    <Windows.h>
  31. #include    <Menus.h>
  32. #include    <Events.h>
  33. #include    <Devices.h>
  34. #include    <DiskInit.h>
  35. #include    <OSUtils.h>
  36. #include    <Resources.h>
  37. #include    <ToolUtils.h>
  38. #include    <AppleEvents.h>
  39. #include    <EPPC.h>
  40. #include    <Gestalt.h>
  41. #include    <Processes.h>
  42. #include    <Balloons.h>
  43. #include    <Aliases.h>
  44. #include    <MixedMode.h>
  45. #include    <Scrap.h>
  46. #include    <LowMem.h>
  47. #include    <Timer.h>
  48. #include    <Serial.h>
  49. #include    <Speech.h>
  50. #include    <TextUtils.h>
  51.  
  52. #include    <string.h>
  53.  
  54. #include "PalmTypes.h"
  55. #include "ErrorBase.h"
  56. #include "ErrorMgr.h"
  57. #include "VFSMgr.h"
  58.  
  59.  
  60. #include "CommandStrings.h"
  61.  
  62. #include "GenericSerial.h"
  63.  
  64.  
  65. #include    "MyUtils.h"
  66. #include    "SerialFSServ.h"
  67.  
  68. #define kMouseRegionWidth    120
  69. #define kMouseRegionHeight    50
  70.  
  71.  
  72. #define        rMonitor        1000
  73. #define        kSendButton        1
  74. #define        kStopButton        2
  75. #define        kQuitButton        3
  76. #define        kMsgBox            4
  77. #define        kSendSpinner    5
  78. #define        kRcvSpinner        7
  79. #define        kHoldFlag        9
  80. #define        kBreakButton    10
  81.  
  82. #define        rSerDataRsrc    128
  83. #define        rSpinnerIcon    1000
  84. #define        rHeldIcon        128
  85. #define        rNotHeldIcon    129
  86.  
  87. #define        rPortOpenALRT    256
  88. #define        kReset            2
  89.  
  90. #define        kCtlEnable        0
  91. #define        kCtlDisable        255
  92. #define        kSerBufSize        16384
  93. #define        kSerRdSize        64
  94. #define        kSerConfig        baud57600 + noParity + data8 + stop10
  95. #define        kBreakLength    666                // serial break length in milliseconds 
  96.  
  97. #define drvrName 0x12                // offset to driver name in 'DRVR' std. header 
  98.  
  99. #define dOpened  5
  100. #define dRAMBased  6
  101. #define drvrActive  7            // Device Manager DCtlFlag bits 
  102.                 
  103. #define kSerStatus  8
  104. #define kSerClrBrk  11            // Serial Driver csCodes 
  105. #define kSerSetBrk  12
  106. #define kSerHShakeDTR  14
  107. #define breakR0  128                // mask for break bit in SCC RR0 -- See TN #56 
  108. #define breakErr  8                    // mask for break bit in cumErrs -- System 7.0 
  109.  
  110. #define kInputDriver "\p.CIn"
  111. #define kOutputDriver "\p.COut"
  112. //#define kInputDriver "\p.IrIn"
  113. //#define kOutputDriver "\p.IrOut"
  114.  
  115.  
  116.  
  117. #define kUpArrowKey        0x7E1E
  118. #define kLeftArrowKey    0x7B1C
  119. #define kDownArrowKey    0x7D1F
  120. #define kRightArrowKey    0x7C1D
  121.  
  122. #define kSpaceBarKey    0x3120
  123. #define kPeriodKey        0x2F2E
  124. #define kEscapeKey        0x351B
  125. #define kEKey            0x0E45
  126. #define kMKey            0x2E4D
  127. #define k0Key            0x1D30
  128. #define k1Key            0x1231
  129. #define k2Key            0x1332
  130. #define k3Key            0x1433
  131. #define kPKey            0x2350
  132.  
  133.  
  134.  
  135. Err PrvTranslateMacErr(OSErr macErr);
  136. Err PrvTranslatePalmPath(const Char *palmPathP);
  137. Err PrvFindDirID(const Char *dirPathNameP, long *dirID);
  138.  
  139.  
  140. #ifdef powerc
  141. //QDGlobals        qd;
  142. #endif
  143.  
  144. Boolean            gDone;            //    Set to true if you want to Application to kindly quit.
  145.  
  146. WindowPtr        gWin = 0;
  147.  
  148. #define kPathBufferLen    600
  149. #define kDirectoryFileRefToken    'dirt'    // FileRef we use for an open directory. Can only have 1 open directory at a time.
  150.  
  151. unsigned char    gPathBuffer[kPathBufferLen];
  152. long            gBaseDirID;
  153. long            gIterateDirID;
  154. short            gCurVRefNum;
  155. Boolean        gDirOpen;
  156. CInfoPBRec    gMyCPB;
  157.  
  158. //GWorldPtr        gRecognizeThis = 0;
  159. unsigned long gLastTicks = 0;
  160.  
  161.  
  162. Ptr            gSerBuf;    // serial driver receive buffer
  163. //            gBitBucket,        // input buffer
  164. //            gOutBuf;    // output buffer
  165.  
  166. IOParam        gSendPB;        // for PBWrite calls to send data
  167. short        gSysVersion,
  168.             gOutRefNum, gInRefNum;
  169.  
  170. long        gBitBucketCount = 0;
  171.  
  172.  
  173.  
  174. void    Initialize (void);
  175. short    SerInitialize (void);
  176. void    CleanUp (void);
  177. Boolean    OpenSERD (void);
  178. void    CloseSERD (void);
  179. OSErr    SendData (void);
  180. void    CheckSerData (void);
  181. void ProcessBitBucket(void);
  182. void EventLoop(char top);
  183.  
  184. /* ------------------------------------------------------------------------- */
  185.  
  186. void main()
  187. {
  188.  
  189.     //    Initialize here.  Set the yield time too.
  190.     // bestTimeBase
  191. //    err = ProfilerInit(collectDetailed,microsecondsTimeBase,50,10);
  192.     Initialize();
  193. //    FindTargetPSN();
  194.     
  195.     if(SerInitialize() != noErr) DebugStr("\pCan't init serial stuff");
  196.     
  197.     // open the Serial Driver 
  198.     if (! OpenSERD()) DebugStr("\pCan't open serial drvr");
  199.  
  200. //        SendData();
  201.         
  202.     
  203.     //    Event loop.
  204.     for ( ; ; ) {
  205.         EventLoop(true);
  206.     }
  207. }
  208.  
  209. void EventLoop(char top)
  210. {
  211.     EventRecord     myEvent;
  212.     long            yieldTime = 0;
  213.     WindowPtr        foundWindow;
  214.     short            windowPart;
  215.     Boolean            isEvent;
  216.     GrafPtr            savePort;
  217.     GDHandle        saveGD;
  218.     OSErr            err=noErr;
  219.     
  220.     //    Get the event.
  221.     isEvent = WaitNextEvent(everyEvent, &myEvent, yieldTime, nil);
  222.     
  223.     if(myEvent.what == keyDown)
  224.     {
  225.         myEvent.what = keyDown;
  226.     }
  227.     
  228. //        CheckSerData();
  229. //        ProcessBitBucket();
  230.     
  231.     if(top) ServeSomething();
  232.     
  233. //        SendEvents();
  234.     
  235.     //    If the event is unhandled by app specific event handling, then we proceed.
  236.     if ( isEvent ) {
  237.         switch ( myEvent.what ) {
  238.  
  239.             case mouseDown:
  240.                 //    Get current port and device.
  241.                 GetPort(&savePort);
  242.                 saveGD = GetGDevice();
  243.                 
  244.                 //    Set the port and gdevice to the window if we own the window.
  245.                 //    We can then assume anytime the event occured in one of our windows,
  246.                 //       that the port and gdevice are set correctly.
  247.                 windowPart = FindWindow(myEvent.where, &foundWindow);
  248.                 SetPort(foundWindow);
  249.                 SetGDevice(GetMainDevice());
  250.                 
  251.                 //    Handle the different mouse down events.
  252.                 switch ( windowPart ) {
  253.                     case inSysWindow:
  254.                         SystemClick(&myEvent, foundWindow);
  255.                         break;
  256.                     case inMenuBar:
  257.                         DoCommand(MenuSelect(myEvent.where));
  258.                         break;
  259.                     case inContent:
  260.                         break;
  261.                     case inDrag:
  262.                         //    If dragging one of the application's windows, then handle it.
  263.                         //    However, if we are dragging a zoomed window, we
  264.                         //    must remember to save the new window location into the
  265.                         //    zoomed rect in the data handle.  Otherwise, the event
  266.                         //    manager will think that we are no longer zoomed.
  267.                         {
  268.                             WStateData    *zoomData;
  269.                             Rect        windowRect;
  270.                             
  271.                             //    Get window location before drag.
  272.                             GetGlobalWindow(foundWindow, &windowRect);
  273.                             
  274.                             //    Drag window.
  275.                             DragWindow (foundWindow, myEvent.where, &qd.screenBits.bounds);
  276. //                                MyDrag(foundWindow, myEvent.where);
  277.  
  278.                             //    If the windowRect in global coordinates matches the zoom rect,
  279.                             //    then assume that we are dragging the zoomed window.  update
  280.                             //    zoom rect.
  281.                             zoomData = (WStateData *) *(((CWindowPeek) foundWindow)->dataHandle);
  282.                             if ( EqualRect(&(zoomData->stdState), &windowRect) ) {
  283.                                 GetGlobalWindow(foundWindow, &windowRect);
  284.                                 zoomData = (WStateData *) *(((CWindowPeek) foundWindow)->dataHandle);
  285.                                 zoomData->stdState = windowRect;
  286.                             }
  287.                         }
  288.                         break;
  289.                     case inGrow:
  290.                         break;
  291.                     case inGoAway:
  292.                         //    Handle clicking on the go away.  If it is the clip window,
  293.                         //    then hide it.
  294.                         if ( TrackGoAway (foundWindow, myEvent.where) ) {
  295.                             BringToFront(foundWindow);
  296. //                                MyClose();
  297.                             DisposeWindow(foundWindow);
  298.                             if(foundWindow == gWin) gWin = 0;
  299.                         }
  300.                         break;
  301.                     case inZoomIn:
  302.                     case inZoomOut:
  303.                         break;
  304.                     default:
  305.                         break;
  306.                 }
  307.                 
  308.                 //    Restore port and device.
  309.                 SetPort(savePort);
  310.                 SetGDevice(saveGD);
  311.                 
  312.                 break;
  313.             case keyDown:
  314.             case autoKey:
  315.                 if ( myEvent.modifiers & cmdKey ) {
  316.                     if ( myEvent.what == keyDown ) {
  317.                         DoCommand(MenuKey(myEvent.message & charCodeMask));
  318.                     }
  319.                 }
  320.                 break;
  321.             case updateEvt:
  322.                 //    Handle update events for window and clip window.
  323.                 foundWindow = (WindowPtr) myEvent.message;
  324.                 GetPort(&savePort);
  325.                 saveGD = GetGDevice();
  326.                 SetPort(foundWindow);
  327.                 SetGDevice(GetMainDevice());
  328.                 BeginUpdate(foundWindow);
  329.                 EndUpdate(foundWindow);
  330.                 SetPort(savePort);
  331.                 SetGDevice(saveGD);
  332.                 break;
  333.             case diskEvt:
  334.                 //    This handles a bad disk.  Otherwise the disk will not eject.
  335.                 if ( myEvent.message >> 16 ) {
  336.                     Point    tempPoint;
  337.                     tempPoint.v = 50; tempPoint.h = 50;
  338.                     DIBadMount(tempPoint, myEvent.message);
  339.                 }
  340.                 break;
  341.             case activateEvt:
  342.                 break;
  343.             case app4Evt:
  344.                 switch ( myEvent.message >> 24 ) {
  345.                     case suspendResumeMessage:
  346. //                            yieldTime = MyYieldTime(myEvent.message & 0x01);
  347.                         break;
  348.                     default:
  349.                         DebugStr("\pUnexpected suspend/resume message.");
  350.                 }
  351.                 break;
  352.             default:
  353.                 break;
  354.         }
  355.     }
  356.  
  357.     //    If DoneFlag set, then quit.
  358.     if ( gDone ) {
  359. //            err = ProfilerDump((unsigned char *)"\pProfileInfo");
  360. //            ProfilerTerm();
  361.         CloseSERD();    // close the Serial Driver 
  362.         CleanUp();
  363.         
  364.         ExitToShell();
  365.     }
  366.  
  367. }
  368.  
  369.  
  370. /* ------------------------------------------------------------------------- */
  371.  
  372. void Initialize()
  373. {
  374. //    OSErr                err;
  375.     Handle                myMenu;
  376. //    Rect bounds;
  377.     
  378.     //    Initialize Managaer.
  379.     MaxApplZone();
  380.     MoreMasters(); MoreMasters();
  381.     MoreMasters(); MoreMasters();
  382.     MoreMasters(); MoreMasters();
  383.     MoreMasters(); MoreMasters();
  384.     InitGraf(&qd.thePort);
  385.     InitFonts();
  386.     FlushEvents(everyEvent, 0);
  387.     InitWindows();
  388.     InitDialogs(nil);
  389.     InitCursor();
  390.  
  391.     //    Set up menus.
  392.     myMenu = GetNewMBar(kMENUBAR);
  393.     SetMenuBar(myMenu);
  394.     DisposeHandle(myMenu);
  395.     AppendResMenu(GetMenuHandle(kMENU_APPLEID), 'DRVR');
  396.     DrawMenuBar();
  397.         
  398.     //    Setup other globals.
  399.     gDone = false;
  400.     
  401. /*    // Create window:
  402.     bounds.top = 50;
  403.     bounds.left = 50;
  404.     bounds.bottom = bounds.top + kMouseRegionHeight;
  405.     bounds.right = bounds.left + kMouseRegionWidth;
  406.     
  407.     gWin = NewCWindow(0, &bounds, "\pBits", true, documentProc, (WindowPtr)-1, true, 0);
  408.     SetPort(gWin);
  409.     
  410.     // Create gworld:
  411.     bounds.top = bounds.left = 0;
  412.     bounds.right = kMouseRegionWidth;
  413.     bounds.bottom = kMouseRegionHeight;
  414.     
  415.     err = NewGWorld(&gRecognizeThis, 16, &bounds, nil, nil, 0);    
  416. */
  417.     gPathBuffer[0] = '\0';
  418.     gDirOpen = false;
  419.     
  420.     return;
  421. }
  422.  
  423. short SerInitialize (void)
  424. {
  425.     long    result;
  426.     OSErr    gestErr;
  427.     
  428.     gestErr = Gestalt(gestaltSystemVersion, &result);
  429.     gSysVersion = result;
  430.     
  431. //    gBitBucket = NewPtr(kSerBufSize);
  432. //    if (gBitBucket == NULL) return MemError();
  433.     
  434.     gSerBuf = NewPtr(kSerBufSize);
  435.     if (gSerBuf == NULL) return MemError();
  436.     
  437. //    gOutBuf = NewPtr(kSerBufSize);
  438. //    if (gOutBuf == NULL) return ResError();
  439.     
  440.     return noErr;
  441. }
  442.  
  443.  
  444. void CleanUp (void)
  445. {
  446.  
  447. //    DisposeHandle((Handle) ghSendPB);
  448. //    DisposePtr(gBitBucket);
  449. //    DisposePtr(gOutBuf);
  450.     DisposePtr(gSerBuf);
  451. }
  452.  
  453.  
  454.  
  455. void DoCommand(long mResult)
  456. {
  457.     short             theMenu, theItem;
  458.     Str255            myStr;
  459.     GrafPtr            savePort;
  460.     GDHandle        saveGD;
  461.     
  462.     theItem = LoWord(mResult);
  463.     theMenu = HiWord(mResult);
  464.     
  465.     if ( theItem != 0 || theMenu != 0 ) {
  466.         switch ( theMenu ) {
  467.             case kMENU_APPLEID:
  468.                 if ( theItem == 1 ) {
  469.                     Alert(kALERT_ABOUT, nil);
  470.                 } else {
  471.                     GetMenuItemText(GetMenuHandle(kMENU_APPLEID), theItem, myStr);
  472.                     GetPort(&savePort);
  473.                     saveGD = GetGDevice();
  474.                     (void) OpenDeskAcc(myStr);
  475.                     SetPort(savePort);
  476.                     SetGDevice(saveGD);
  477.                 }
  478.                 break;
  479.     
  480.             case kMENU_FILEID:
  481.                 switch ( theItem ) {
  482.                     case kMENU_FILENEW:
  483. //                        MyNew();
  484.                         break;
  485.                     case kMENU_FILECLOSE:
  486. //                        MyClose();
  487.                         break;
  488.                     case kMENU_FILEQUIT:
  489.                         gDone = true;
  490.                         break;
  491.                     default:
  492.                         ReportFatal("\pError in handling file menu:", theItem);
  493.                 }
  494.                 break;
  495.         
  496.             default:
  497.                 ReportFatal("\pError in handling menu:", theMenu);
  498.         }
  499.     }
  500.     HiliteMenu(0);
  501. }
  502.  
  503.  
  504.  
  505.  
  506. Boolean OpenSERD (void)
  507. {
  508.     OSErr    openOutErr, openInErr;
  509.     OSErr    setBufErr, setCfgErr, setHskErr;
  510.     SerShk    hskFlags;
  511. //    long    finalTicks;
  512.     Boolean    takeOverPort = true;
  513. //    Boolean    openAOut, openAIn;
  514.     
  515. /*    openAOut = AssertDrvrOpen(kOutputDriver, &gOutRefNum) == noErr;
  516.     openAIn = AssertDrvrOpen(kInputDriver, &gInRefNum) == noErr;
  517.     if (openAOut || openAIn) {
  518.         if (takeOverPort = CautionAlert(rPortOpenALRT, nil) == kReset) {
  519.             if (openAIn) {
  520.                 KillIO(gInRefNum);
  521.                 CloseDriver(gInRefNum);
  522.             }
  523.             if (openAOut) {
  524.                 KillIO(gOutRefNum);
  525.                 CloseDriver(gOutRefNum);
  526.             }
  527.         }
  528.     }
  529.     
  530.     if (takeOverPort) {
  531. */
  532.         openOutErr = OpenDriver(kOutputDriver, &gOutRefNum);
  533.         
  534. //        if(openOutErr == portInUse)
  535. //        {
  536. //            KillIO(gOutRefNum);
  537. //            CloseDriver(gOutRefNum);
  538. //            openOutErr = OpenDriver(kOutputDriver, &gOutRefNum);
  539. //        }
  540.         
  541.         openInErr = OpenDriver(kInputDriver, &gInRefNum);
  542. //        if(openInErr == portInUse)
  543. //        {
  544. //            KillIO(gInRefNum);
  545. //            CloseDriver(gInRefNum);
  546. //            openInErr = OpenDriver(kInputDriver, &gInRefNum);
  547. //        }
  548.         
  549.         if (openOutErr == noErr && openInErr == noErr) {
  550.             
  551.             // It's always good to first set a non-default input buffer, if desired. 
  552.             // There is no output buffering, so specify only the input driver.       
  553.             
  554.             setBufErr = SerSetBuf(gInRefNum, gSerBuf, kSerBufSize);
  555.             
  556.             hskFlags.fXOn = false;
  557.             hskFlags.fCTS = true;
  558.             hskFlags.xOn = 0x11;
  559.             hskFlags.xOff = 0x13;
  560.             hskFlags.errs = 0;
  561.             
  562.             if (gSysVersion >= 0x0700) {
  563.                 hskFlags.evts = 0;                // I can use new means of break detection. 
  564.             }
  565.             else {
  566.                 hskFlags.evts = breakEvent;        // I need the driver to post break events. 
  567.             }
  568.             
  569.             hskFlags.fInX = false;
  570.             hskFlags.fDTR = true;
  571.             
  572.             // SerHShake() does not support full DTR/CTS hardware handshaking. You   
  573.             // accomplish the same thing and more with a Control call and csCode 14. 
  574.             // You only need to specify hskFlags once, to the output driver.         
  575.             
  576.             setHskErr = Control(gOutRefNum, kSerHShakeDTR, (Ptr) &hskFlags);
  577.  
  578.             // Now reset both input and output drivers with the same configuration.  
  579.             // Only a single call to the output driver is necessary to do this.      
  580.             // Differing concurrent input/output baud rates are not supported.       
  581.             
  582.             setCfgErr = SerReset(gOutRefNum, kSerConfig);
  583.             
  584.         }
  585.         else DebugStr("\pCan't open driver");
  586. //    }
  587.     
  588.     return takeOverPort;
  589. }
  590.  
  591.  
  592.  
  593. void CloseSERD (void)
  594. {
  595.     OSErr    killErr, closeOutErr, closeInErr;
  596.  
  597.     killErr = KillIO(gInRefNum);
  598.     closeInErr = CloseDriver(gInRefNum);
  599.  
  600.     killErr = KillIO(gOutRefNum);
  601.     closeOutErr = CloseDriver(gOutRefNum);
  602.  
  603. }
  604.  
  605.         
  606. /*
  607.  
  608. void CheckSerData (void)
  609. {
  610.     OSErr    checkBufErr, serRdErr;
  611.     long    charCount;
  612.         
  613.     checkBufErr = SerGetBuf(gInRefNum, &charCount);
  614.     if (checkBufErr == noErr) {
  615.     
  616.         // The general strategy here is this: if number of available characters 
  617.         // meets a certain minimum threshold, then I read in everything in the  
  618.         // buffer. If I get delayed, I'll catch up quickly.                     
  619.  
  620. //        if (charCount != 0 && charCount >= reqBytes) {
  621.         if (charCount > 0) {
  622.             
  623.             serRdErr = FSRead(gInRefNum, &charCount, &gBitBucket[gBitBucketCount]);
  624.             if (serRdErr == noErr) {
  625.                 gBitBucketCount += charCount;
  626. //                gBitBucket[gBitBucketCount] = 0xFF; // append an 'unfinished string' terminator
  627.             }
  628.         }
  629.     }
  630. }
  631. */
  632. /*
  633. OSErr SendData (void)
  634. {
  635.     gSendPB.ioCompletion = 0;//(ProcPtr)0;// SendCompRout;
  636.     gSendPB.ioRefNum = gOutRefNum;
  637.     gSendPB.ioBuffer = "Twas Brillig & the Slithy Toves";//gpOutBuf;
  638.     gSendPB.ioReqCount = 31;//kSerBufSize;
  639.     
  640. //    return PBWriteAsync((ParmBlkPtr) gSendPB);            // asynchronous self-sustaining sends 
  641.     return PBWriteSync((ParmBlkPtr) &gSendPB);            // asynchronous self-sustaining sends 
  642.  
  643. }
  644. */
  645. /*
  646.     Searches the receive buffer, looking for 
  647.     completed (\r terminated) command strings to execute.
  648. */
  649. /*
  650. void ProcessBitBucket(void)
  651. {
  652.     char* s = gBitBucket;
  653.     short i = 0;
  654.     Boolean foundString = false;
  655.     
  656.     while(foundString == false && i < gBitBucketCount)
  657.     {
  658.         if(s[i] == '\r')
  659.         {
  660.             foundString = true;
  661.             s[i] = 0; // null terminate the string
  662.         }
  663.         if(s[i] == 0xFF)
  664.         {
  665.             foundString = false;
  666.             break;
  667.         }
  668.         
  669.         i++;
  670.     }
  671.     
  672.     if(foundString == true) 
  673.     {
  674.         if(strcmp(s, "Prev") == 0) {
  675.             s = "Previous";
  676.         }
  677.         c2pstr(s);
  678.         SpeakString((unsigned char*)s);
  679.         p2cstr((unsigned char*)s);
  680.         
  681. //        ProcessString(s);
  682.         
  683.         gBitBucketCount -= i;
  684.         BlockMove(&s[i], &s[0], gBitBucketCount);
  685.     }
  686.     
  687.     return;
  688. }
  689.  
  690. */
  691. /*
  692.     Given a command string, set the right 
  693.     event up to be posted to the CD/DVD player
  694. */
  695. /*
  696. void ProcessString(char* s)
  697. {
  698.  
  699.     if(strcmp(s, kPlayCmd) == 0 || 
  700.         strcmp(s, kPauseCmd) == 0)
  701.     {
  702. //        PostEvent(keyDown, ' ');
  703.         gMessageToPost = kSpaceBarKey;    // ' '
  704.         gMessageModifiers = 0;
  705.     }
  706.     else if(strcmp(s, kStopCmd) == 0)
  707.     {
  708.         gMessageToPost = kPeriodKey;    // cmd-.
  709.         gMessageModifiers = cmdKey;
  710.     }
  711.     else if(strcmp(s, kFastFwdCmd) == 0)
  712.     {
  713.         gMessageToPost = kRightArrowKey;    // cmd ->
  714.         gMessageModifiers = cmdKey;
  715.     }
  716.     else if(strcmp(s, kRewindCmd) == 0)
  717.     {
  718.         gMessageToPost = kLeftArrowKey;    // cmd <-
  719.         gMessageModifiers = cmdKey;
  720.     }
  721.     else if(strcmp(s, kNextCmd) == 0)
  722.     {
  723.         gMessageToPost = kRightArrowKey;    // -> arrow key
  724.         gMessageModifiers = 0;
  725.     }
  726.     else if(strcmp(s, kPrevCmd) == 0)
  727.     {
  728.         gMessageToPost = kLeftArrowKey;    // <- arrow key
  729.         gMessageModifiers = 0;
  730.     }
  731.     else if(strcmp(s, kEjectCmd) == 0)
  732.     {
  733.         gMessageToPost = kEKey;    // cmd-E
  734.         gMessageModifiers = cmdKey;
  735.     }
  736.     else if(strcmp(s, kVolUpCmd) == 0)
  737.     {
  738.         gMessageToPost = kUpArrowKey;    // cmd-up
  739.         gMessageModifiers = cmdKey;
  740.     }
  741.     else if(strcmp(s, kVolDownCmd) == 0)
  742.     {
  743.         gMessageToPost = kDownArrowKey;    // cmd-down
  744.         gMessageModifiers = cmdKey;
  745.     }
  746.     else if(strcmp(s, kMuteCmd) == 0)
  747.     {
  748.         gMessageToPost = kMKey;    // cmd-M
  749.         gMessageModifiers = cmdKey;
  750.     }
  751.     else if(strcmp(s, kDVDMenuCmd) == 0)
  752.     {
  753.         gMessageToPost = kEscapeKey;    // escape
  754.         gMessageModifiers = 0;
  755.     }
  756.     else if(strcmp(s, kSizeTinyCmd) == 0)
  757.     {
  758.         gMessageToPost = k0Key;    // cmd-0
  759.         gMessageModifiers = cmdKey;
  760.     }
  761.     else if(strcmp(s, kSizeHalfCmd) == 0)
  762.     {
  763.         gMessageToPost = k1Key;    // cmd-1
  764.         gMessageModifiers = cmdKey;
  765.     }
  766.     else if(strcmp(s, kSizeNormCmd) == 0)
  767.     {
  768.         gMessageToPost = k2Key;    // cmd-2
  769.         gMessageModifiers = cmdKey;
  770.     }
  771.     else if(strcmp(s, kSizeFullCmd) == 0)
  772.     {
  773.         gMessageToPost = k3Key;    // cmd-3
  774.         gMessageModifiers = cmdKey;
  775.     }
  776.     else if(strcmp(s, kPresentCmd) == 0)
  777.     {
  778.         gMessageToPost = kPKey;    // cmd-option-P
  779.         gMessageModifiers = cmdKey | optionKey;
  780.     }
  781.     
  782.     return;
  783. }
  784. */
  785. // -------------------
  786.  
  787. short SerialRead(short numBytes, char *bufP)
  788. {
  789.     OSErr    checkBufErr, err;
  790.     long    charCount = 0;
  791. //    long    lastCount = TickCount();
  792. //    EventRecord    myEvent;
  793.     
  794.     while(charCount < numBytes)
  795.     {
  796.         //WaitNextEvent(0, &myEvent, 5, nil);
  797.         EventLoop(false);
  798.         
  799.         checkBufErr = SerGetBuf(gInRefNum, &charCount);
  800.         
  801. //        if((TickCount() - lastCount) > (60*60))
  802. //        {
  803. //            Debugger();
  804. //            lastCount = TickCount();
  805. //        }
  806.         
  807.     }
  808.     
  809.     charCount = numBytes;
  810.     
  811.     err = FSRead(gInRefNum, &charCount, bufP);
  812.     
  813.     if(numBytes == sizeof(MyPacket) && 
  814.         ((*(long*)bufP) & 0x00FFFFFF) == '\0hac')
  815.     {
  816.         memmove(bufP, bufP+1, sizeof(MyPacket)-1);
  817.         charCount = 1;
  818.         err = FSRead(gInRefNum, &charCount, bufP+ sizeof(MyPacket)-1);
  819.     }
  820.     
  821.     return err;
  822.     
  823. /*    checkBufErr = SerGetBuf(gInRefNum, &charCount);
  824.     if (checkBufErr == noErr) {
  825.     
  826.         // The general strategy here is this: if number of available characters 
  827.         // meets a certain minimum threshold, then I read in everything in the  
  828.         // buffer. If I get delayed, I'll catch up quickly.                     
  829.  
  830. //        if (charCount != 0 && charCount >= reqBytes) {
  831.         if (charCount > 0) {
  832.             
  833.             serRdErr = FSRead(gInRefNum, &charCount, &gBitBucket[gBitBucketCount]);
  834.             if (serRdErr == noErr) {
  835.                 gBitBucketCount += charCount;
  836. //                gBitBucket[gBitBucketCount] = 0xFF; // append an 'unfinished string' terminator
  837.             }
  838.         }
  839.         }
  840.     }*/
  841. }
  842.  
  843. short SerialWrite(short numBytes, char *bufP)
  844. {
  845.     long    charCount = numBytes;
  846.     
  847.     return FSWrite(gOutRefNum, &charCount, bufP);
  848.     
  849. }
  850.  
  851.  
  852. Err PrvTranslateMacErr(OSErr macErr)
  853. {
  854.     Err    err;
  855.     
  856.     switch (macErr)
  857.     {
  858.         case noErr:            err = errNone;                            break;
  859.         case fnfErr:        err = vfsErrFileNotFound;            break;
  860.         case eofErr:        err = vfsErrFileEOF;                    break;
  861.         case dupFNErr:        err = vfsErrFileAlreadyExists;    break;
  862.         
  863.         default:
  864.             err = -1;
  865.             break;
  866.     }
  867.     
  868.     return err;
  869. }
  870.  
  871. Err PrvTranslatePalmPath(const Char *palmPathP)
  872. {
  873.     unsigned char*    dstP;
  874.     
  875.     if(strlen(palmPathP) >= (kPathBufferLen - 1)) return -1;
  876.     
  877.     // clear string:
  878.     gPathBuffer[0]=0;
  879.     
  880.     // start writing after length byte:
  881.     dstP = &gPathBuffer[1];
  882.     
  883.     // if there's a leading slash, remove it... we want to go from the root.
  884. //    if(palmPathP[0] == '/') 
  885. //        palmPathP++;
  886.     
  887.     // copy characters, translating '/' to ':', and incrementing length.
  888.     while(*palmPathP != '\0')
  889.     {
  890.         if(*palmPathP == '/')
  891.         {
  892.             *dstP = ':';
  893.         }
  894.         else
  895.         {
  896.             *dstP = *palmPathP;
  897.         }
  898.         dstP++;
  899.         palmPathP++;
  900.         gPathBuffer[0]++;
  901.     }
  902.     
  903.     
  904.     return errNone;
  905. }
  906.  
  907. short LocalDirIterate(long ref, unsigned long *dirEntryP, char *buf, long *size, long *attr)
  908. {
  909.     short index;
  910.     Err    err;
  911.     OSErr    macErr;
  912.     unsigned char    nameBuf[32];
  913. //    long    dirID;
  914.     short    len;
  915.     
  916.     if(*dirEntryP == expIteratorStop) 
  917.             return expErrEnumerationEmpty;
  918.     
  919. //    err = PrvFindDirID(dirPathNameP, &dirID);
  920. //    if(err) return err;
  921.     
  922.     // make sure its a directory:
  923.     if(gDirOpen == false) return vfsErrFileBadRef;
  924.     if(ref != kDirectoryFileRefToken) return vfsErrNotADirectory;
  925.     
  926.     
  927.     memset(&gMyCPB, 0x00, sizeof(gMyCPB));    // clear param block
  928.     
  929.     gPathBuffer[0] = 0;
  930.     gMyCPB.hFileInfo.ioCompletion = NULL;
  931.     
  932.     gMyCPB.hFileInfo.ioNamePtr = nameBuf;
  933.     gMyCPB.hFileInfo.ioFRefNum = 0;
  934.     
  935.     // Get index from iterator value:
  936.     index = (short)*dirEntryP;
  937.     if(index == 0) index = 1;    // if its the 1st time through, bump the index to 1 since its actually 1-based.
  938.     
  939.     gMyCPB.hFileInfo.ioFDirIndex = index;
  940.     gMyCPB.hFileInfo.ioVRefNum = gCurVRefNum;
  941.     gMyCPB.hFileInfo.ioDirID = gIterateDirID;//gBaseDirID;
  942.     
  943.     macErr = PBGetCatInfo(&gMyCPB, false);
  944.     err = PrvTranslateMacErr(macErr);
  945.     
  946.     if(err == errNone)
  947.     {
  948.         // convert name to c string:
  949.         len = nameBuf[0];
  950.         memmove(&nameBuf[0], &nameBuf[1], len);
  951.         nameBuf[len] = '\0';
  952.         
  953.         // copy name:
  954.         strcpy(buf, (char*)nameBuf);
  955.         *size = len;
  956.         
  957.         // Fill out attributes:
  958.         *attr = 0;
  959.         if(gMyCPB.hFileInfo.ioFlAttrib & 0x0001) *attr |= vfsFileAttrReadOnly;
  960.         if(gMyCPB.hFileInfo.ioFlAttrib & 0x0010) *attr |= vfsFileAttrDirectory;
  961.     }
  962.     
  963.     index++;    // point index to next file
  964.     
  965.     // make another call to see if we're at the end...
  966.     
  967.     memset(&gMyCPB, 0x00, sizeof(gMyCPB));    // clear param block...
  968.     
  969.     gMyCPB.hFileInfo.ioCompletion = NULL;
  970.     gMyCPB.hFileInfo.ioNamePtr = nameBuf;
  971.     gMyCPB.hFileInfo.ioFRefNum = 0;
  972.     
  973.     gMyCPB.hFileInfo.ioFDirIndex = index;
  974.     gMyCPB.hFileInfo.ioVRefNum = gCurVRefNum;
  975.     gMyCPB.hFileInfo.ioDirID = gIterateDirID;
  976.     
  977.     macErr = PBGetCatInfo(&gMyCPB, false);
  978.     
  979.     
  980.     if(macErr) *dirEntryP = expIteratorStop;
  981.     else *dirEntryP = index;
  982.     
  983.     return err;
  984. }
  985.  
  986. short LocalFileCreate(char *path)
  987. {
  988.     OSErr macErr;
  989.     Err    err;
  990.     
  991.     PrvTranslatePalmPath(path);
  992.     
  993.     macErr = Create(gPathBuffer, 0, 'PALM', 'File');
  994.     err = PrvTranslateMacErr(macErr);
  995.     
  996.     return err;
  997. }
  998.  
  999. short LocalDirCreate(char *path)
  1000. {
  1001.     OSErr macErr;
  1002.     Err    err;
  1003.     long        dirID;
  1004.     
  1005.     PrvTranslatePalmPath(path);
  1006.     
  1007. //    FSMakeFSSpec(0, 0, gPathBuffer, &spec);
  1008.     macErr = DirCreate(0,0,gPathBuffer,&dirID);
  1009.     err = PrvTranslateMacErr(macErr);
  1010.     
  1011.     return err;
  1012. }
  1013.  
  1014. // given a full pathname to a directory, find the dirID
  1015. Err PrvFindDirID(const Char *dirPathNameP, long *dirID)
  1016. {
  1017.     Err err;
  1018.     
  1019.     PrvTranslatePalmPath(dirPathNameP);
  1020.     
  1021.     gMyCPB.hFileInfo.ioCompletion = NULL;
  1022.     gMyCPB.hFileInfo.ioNamePtr = gPathBuffer;
  1023.     gMyCPB.hFileInfo.ioFRefNum = 0;
  1024.     gMyCPB.hFileInfo.ioFDirIndex = 0;
  1025.     gMyCPB.hFileInfo.ioVRefNum = gCurVRefNum;
  1026.     gMyCPB.hFileInfo.ioDirID = gBaseDirID;
  1027.     
  1028.     err = PrvTranslateMacErr(PBGetCatInfo(&gMyCPB, false));
  1029.     
  1030.     if(err == errNone)
  1031.     {
  1032.         if(gMyCPB.hFileInfo.ioFlAttrib & 0x0010) *dirID = gMyCPB.hFileInfo.ioDirID;
  1033.         else err = vfsErrNotADirectory;
  1034.     }
  1035.     
  1036.     return err;    
  1037. }
  1038.  
  1039. short LocalFileOpen(char *path, long /*perm*/, long *fileRefP)
  1040. {
  1041.     Err err = PrvTranslatePalmPath(path);
  1042.     OSErr macErr;
  1043.     short        macRef;
  1044.     long        dirID;
  1045.     
  1046.     if(err) return err;
  1047.     
  1048.     err = PrvFindDirID(path, &dirID);
  1049.     if(err == errNone) 
  1050.     {
  1051.         // if iterating root, save path:
  1052. //        if(StrCompare(pathNameP, "/Palm/Launcher") == 0)
  1053. //        {
  1054.             if(gDirOpen)
  1055.             {
  1056.                 return vfsErrFileStillOpen;
  1057.             }
  1058.             
  1059.             macErr = HGetVol(NULL, &gCurVRefNum, &gBaseDirID);
  1060.             err = PrvTranslateMacErr(macErr);
  1061.             if(err) 
  1062.             {
  1063.                 gBaseDirID = 0;
  1064.                 gCurVRefNum = 0;
  1065.                 return err;
  1066.             }
  1067.             
  1068.             gIterateDirID = dirID;
  1069.             
  1070.             *fileRefP = kDirectoryFileRefToken;
  1071.             gDirOpen = true;
  1072.             return errNone;
  1073. //        }
  1074.     }
  1075.     
  1076.     macErr = FSOpen(gPathBuffer, 0, &macRef);
  1077.     err = PrvTranslateMacErr(macErr);
  1078.     
  1079.     // DOLATER-jesse: fix this to work with directories other than the root...
  1080.     
  1081.     if(err == errNone) *fileRefP = macRef;
  1082.     else *fileRefP = 0;
  1083.     
  1084.     return err;
  1085. }
  1086.  
  1087. short LocalFileClose(long filep)
  1088. {
  1089.     if(filep == kDirectoryFileRefToken && gDirOpen == true)
  1090.     {
  1091.         gDirOpen = false;
  1092.         gIterateDirID = 0;
  1093.         return errNone;
  1094.     }
  1095.     
  1096.     return PrvTranslateMacErr(FSClose((short)filep));
  1097. }
  1098.  
  1099. short LocalFileRead(long fileRef, long numBytes, long *numBytesRead, char *bufP)
  1100. {
  1101.     Int32    count = (Int32)numBytes;
  1102.     Err err;
  1103.     
  1104.     err = PrvTranslateMacErr(FSRead(fileRef, &count, bufP));
  1105.     if(numBytesRead) *numBytesRead = count;
  1106.     
  1107.     return err;
  1108. }
  1109.  
  1110.  
  1111. short LocalFileWrite(long fileRef, long numBytes, long *numWritten, char *bufP)
  1112. {
  1113.     Int32    count = (Int32)numBytes;
  1114.     Err     err;
  1115.     
  1116.     err = PrvTranslateMacErr(FSWrite(fileRef, &count, bufP));
  1117.     if(numWritten) *numWritten = count;
  1118.     
  1119.     return err;
  1120. }
  1121.  
  1122.  
  1123. short LocalFileSeek(long fileRef, short origin, unsigned short offset)
  1124. {
  1125.     short mode;
  1126.     
  1127.     switch(origin)
  1128.     {
  1129.         case vfsOriginBeginning:    
  1130.             mode = 1; 
  1131.             break;
  1132.         case vfsOriginEnd:            
  1133.             mode = 2; 
  1134.             break;
  1135.         case vfsOriginCurrent:    
  1136.             mode = 3; 
  1137.             break;
  1138.         
  1139.         default:
  1140.             DebugStr("\pinvalid FileOrigin");
  1141.     }
  1142.     
  1143.     return PrvTranslateMacErr(SetFPos((short)fileRef, mode, offset));
  1144. }
  1145.  
  1146. short LocalFileSize(long fileRef, long *size)
  1147. {
  1148.     OSErr osErr = GetEOF(fileRef, size);
  1149.     
  1150.     return PrvTranslateMacErr(osErr);
  1151. }
  1152.  
  1153. short LocalFileResize(long fileRef, long newsize)
  1154. {
  1155.     OSErr osErr = SetEOF(fileRef, newsize);
  1156.     
  1157.     return PrvTranslateMacErr(osErr);
  1158. }
  1159.  
  1160. short LocalFileDelete(char *path)
  1161. {
  1162.     OSErr macErr;
  1163.     Err    err;
  1164.     
  1165.     PrvTranslatePalmPath(path);
  1166.     
  1167.     macErr = HGetVol(NULL, &gCurVRefNum, &gBaseDirID);
  1168.     err = PrvTranslateMacErr(macErr);
  1169.     if(err) 
  1170.     {
  1171.         gBaseDirID = 0;
  1172.         gCurVRefNum = 0;
  1173.         return err;
  1174.     }
  1175.     
  1176.     macErr = HDelete(gCurVRefNum, gBaseDirID, gPathBuffer);
  1177.     err = PrvTranslateMacErr(macErr);
  1178.     
  1179.     return err;
  1180. }
  1181.